package com.cykj.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cykj.dto.ResponseDTO;
import com.cykj.feign.GoodFeign;
import com.cykj.pojo.User;
import com.cykj.service.ImageCodeUtils;
import com.cykj.service.UserService;
import com.cykj.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RefreshScope
public class UserController {
    @Value("${cykj.code.width}")
    private String codeWidth;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.redis.host}")
    private String host;
    @Autowired
    private GoodFeign goodFeign;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ImageCodeUtils imageCodeUtils ;
@Autowired
   private RestTemplate restTemplate;
    @Autowired
    private UserService userService;
    @RequestMapping("/getCode")
    public void getCode(OutputStream os){
        try {
            System.out.println(imageCodeUtils.getCode());
            imageCodeUtils.createImage();
            imageCodeUtils.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/getGoodClass")
    public ResponseDTO getGoodClass(){

   return userService.getGoodClass();
    }
    @RequestMapping("/getGoodClassType")
    public ResponseDTO getGoodClassType(@RequestParam String id){

        return userService.getGoodClassType(id);
    }
    @RequestMapping("/getGoods")
    public ResponseDTO getGoods(@RequestParam String id){

        return userService.getGoods(id);
    }

    @RequestMapping("/index")
    public String index(){
        return "hello";
    }
    @RequestMapping("/getUserInfo")
    public ResponseDTO getUserInfo(){
        return ResponseDTO.success(1);
    }
//    @RequestMapping("/login")
//        public ResponseDTO login(@RequestBody User user){
//        System.out.println(user);
//            if(user.getUsername().equals("admin")&&user.getPassword().equals("123456")){
//               HashMap<String,Object> map =new HashMap<>();
//               map.put("userId",1);
//                String token=JwtUtils.generateJwt(map);
//                JSONObject jsonObject=new JSONObject();
//                jsonObject.put("姓名","佳佳");
//                jsonObject.put("sex","boy");
//                jsonObject.put("age","24");
//                redisTemplate.opsForValue().set(token,jsonObject.toJSONString());
//                redisTemplate.expire(token,30, TimeUnit.SECONDS);
//                return ResponseDTO.success(token);
//
//        }
//            else{
//                return ResponseDTO.error("账号或密码错误");
//            }
//
//    }

    @RequestMapping("/hotGoodsToRedis")
    public void hotGoodsToRedis() {
        ResponseDTO hotGoods = userService.getHotGoods();
        System.out.println(hotGoods);
    }
    @RequestMapping("/testGood")
    public String testGood() {
//        String forObject = restTemplate.getForObject("http://jd-good/test", String.class);
//        return forObject;
        return goodFeign.test();

    }
    @RequestMapping("/testConfig")
    public ResponseDTO testConfig() {
        System.out.println(url);
        return userService.getHotGoods() ;

    }

    @RequestMapping("/uLogin")
    public String login() {
        return "login-";
    }
    @RequestMapping("/login")
    public String login1() {
        return "login-";
    }
    @RequestMapping("/testHost")
    public String testHost() {
return host;
    }

}
